 /**
 * User: yeqingzhou01
 * Date: 13-10-20
 * Time: 下午11:21
 */
var oneDayMilliseconds = 86400000;

var util = require('util')
var oneHourMilliseconds = 3600000;

var datePatten = /^(\d{4})\-(\d{2})\-(\d{2})$/;
var dateTimePatten = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;

exports.AniDate = function (oneDateObj) {
    var localOffsetMilliseconds = oneDateObj.getTimezoneOffset() * 60 * 1000;
    var _this = this;
    _this.currentTime = oneDateObj;
    _this.today = new Date(
        _this.currentTime.getTime()
            -
            (_this.currentTime.getTime() - localOffsetMilliseconds)
                % oneDayMilliseconds

    );

    _this.yesterday = new Date(_this.today.getTime() - oneDayMilliseconds);

    _this.tomorrow = new Date(_this.today.getTime() + oneDayMilliseconds);

    _this.toDayTimeSpanArr = function () {
        return [
            new Date(exports.AniDate.getDateStr(_this.currentTime) + ' 00:00:00'),
            new Date(exports.AniDate.getDateStr(_this.tomorrow) + ' 00:00:00')
        ];
    }

    _this.getWithOffsetFromToday = function (offsetDayNum) {
        new Date(_this.today.getTime() + offsetDayNum * oneDayMilliseconds);
    }

}
exports.AniDate.generateFromStr = function (dateOrTimeStr) {
    if (dateOrTimeStr != null && dateOrTimeStr instanceof String) {
        dateOrTimeStr = dateOrTimeStr.trim();
        if (dateOrTimeStr.split(' ').length > 1 && dateOrTimeStr.contains(':')) {
            return new Date(dateOrTimeStr);
        } else {
            return [
                new Date(dateOrTimeStr),
                new exports.AniDate(new Date(dateOrTimeStr)).tomorrow
            ];
        }
    }
};
exports.AniDate.getStandardTimeFromStr = function (standardTimeStr) {
    if (typeof standardTimeStr == 'string') {
        standardTimeStr = standardTimeStr.trim();
    } else {
        util.error('INVALID TIME STR: ' + standardTimeStr)
        standardTimeStr = '1970-01-01';
    }

    if (exports.AniDate.isDate(standardTimeStr)) {
        return new Date(standardTimeStr + ' 00:00:00');
    } else if (exports.AniDate.isDateTime(standardTimeStr)) {
        return new Date(standardTimeStr);
    } else {
        return new Date(standardTimeStr);
    }
}
exports.AniDate.isDate = function (testStr) {
    return datePatten.test(testStr.trim());
}
exports.AniDate.isDateTime = function (testStr) {
    return dateTimePatten.test(testStr.trim());
}
exports.AniDate.isDateOrDateTime = function (testStr) {
    return datePatten.test(testStr.trim()) || dateTimePatten.test(testStr.trim());
}
var getTimeSegmentStrWithZero = function (oneDateSegmentNum) {
    return oneDateSegmentNum >= 10 ? oneDateSegmentNum : '0' + oneDateSegmentNum;
}
exports.AniDate.getDateStr = function (oneDateObj) {
    return oneDateObj.getFullYear() + '-'
        + getTimeSegmentStrWithZero(oneDateObj.getMonth() + 1) + '-'
        + getTimeSegmentStrWithZero(oneDateObj.getDate());
}
exports.AniDate.getDateTimeStr = function (oneDateObj) {
    return exports.AniDate.getDateStr(oneDateObj) + ' '
        + getTimeSegmentStrWithZero(oneDateObj.getHours()) + ':'
        + getTimeSegmentStrWithZero(oneDateObj.getMinutes()) + ':'
        + getTimeSegmentStrWithZero(oneDateObj.getSeconds())
}
exports.AniDate.getDateSpan = function (timeSpanArr) {
    var dateStrList = [];
    for(var dateStrIdx in timeSpanArr) {
        if(typeof timeSpanArr[dateStrIdx] == 'string'){
            dateStrList.push(timeSpanArr[dateStrIdx].trim());
        }
    }
    dateStrList.sort();
    if(dateStrList.length >= 2){
        return [
            new exports.AniDate(new Date(dateStrList[0])).toDayTimeSpanArr()[0],
            new exports.AniDate(new Date(dateStrList[dateStrList.length - 1])).toDayTimeSpanArr()[1]
        ];
    } else if(dateStrList.length == 1) {
        return new exports.AniDate(
            new Date(dateStrList[0])
        ).toDayTimeSpanArr();
    }
    return dateStrList;
}
var turnStrArrToNumber = function (strArr) {
    for (var oneIdx in strArr) {
        if (strArr[oneIdx] instanceof String) {
            strArr[oneIdx] = parseInt(strArr[oneIdx]);
        }
    }
}
exports.AniDate.getFromArr = function (dateTimeArr) {
    turnStrArrToNumber(dateTimeArr);
    return new Date(
        dateTimeArr[0],
        dateTimeArr[1],
        dateTimeArr[2],
        dateTimeArr[3],
        dateTimeArr[4],
        dateTimeArr[5],
        dateTimeArr[6]
    );
}
